﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HNG.Portal.BLL;
using HNG.Portal.DAL;
using HNG.Portal.DAL.EntityClasses;
using HNG.Portal.DAL.FactoryClasses;
using HNG.Portal.DAL.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
using System.Web.UI.HtmlControls;
using System.Data;
using System.IO;
public partial class AdminPortal_Controls_SanPham_PhuKien_Info : System.Web.UI.UserControl
{
    TblSanPhamPhuKienManager _TblSanPhamPhuKienManager = new TblSanPhamPhuKienManager();
    public int CateID = 0;
    string LangID = HNG.LIB.Localization.LocalizationUtils.GetCurrentLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try { CateID = Convert.ToInt32(Request.QueryString["id"].Trim()); }
            catch { CateID = 0; }

            LoadDanhMucSanPham();

            if (CateID == 0) //Add cate
            {
                lblTitle.Text = "Thêm phụ kiện";
                Toolbox1.ShowBack = Toolbox1.ShowRefesh = Toolbox1.ShowDel = false;
            }
            else //sua cate
            {
                lblTitle.Text = "Sửa phụ kiện: " + CateID;
                BindData(CateID);
            }
        }
    }

    protected void BindData(int ID)
    {
        TblSanPhamPhuKienEntity _TblSanPhamPhuKienEntity = _TblSanPhamPhuKienManager.SelectOne(ID);
        if (_TblSanPhamPhuKienEntity == null) { lblthongbao.Text = "<img src='../../Images/validatebox_warning.png' alt='Cảnh báo' width='16' height='16' /> <span style='font-size:11px;color:red;'>  Không có danh mục này </span>"; return; }
        hidID.Value = ID.ToString();
        txtID.Text = ID.ToString();
        txtName.Text = Server.HtmlDecode(_TblSanPhamPhuKienEntity.TenPhuKien);
        txtGia.Text = _TblSanPhamPhuKienEntity.GiaBan.ToString();
        txtSoLuong.Text = _TblSanPhamPhuKienEntity.SoLuong.ToString();
        chkGiaBan.Checked = Convert.ToBoolean(_TblSanPhamPhuKienEntity.SuDungGia);
        chkStatus.Checked = Convert.ToBoolean(_TblSanPhamPhuKienEntity.TrangThai);
        ddlDanhMucSanPham.SelectedValue = _TblSanPhamPhuKienEntity.DanhMucSanPhamId.ToString();
        ddlDanhMucSanPham_SelectedIndexChanged(new object(), new EventArgs());
        if (listSanPham.Items.Count > 0)
        {
            listSanPham.SelectedValue = _TblSanPhamPhuKienEntity.SanPhamId.ToString();
        }
    }

    private void LoadDanhMucSanPham()
    {
        TblDanhMucSanPhamEntity _TblDanhMucSanPhamEntity = new TblDanhMucSanPhamEntity();
        _TblDanhMucSanPhamEntity.LangId = LangID;
        DataTable _dtCha = new TblDanhMucSanPhamManager().be_SelectToGrivew(_TblDanhMucSanPhamEntity);
        if (_dtCha != null)
        {
            ddlDanhMucSanPham.DataTextField = TblDanhMucSanPhamFields.TenDanhMuc.Name;
            ddlDanhMucSanPham.DataValueField = TblDanhMucSanPhamFields.Id.Name;
            ddlDanhMucSanPham.DataSource = _dtCha;
            ddlDanhMucSanPham.DataBind();
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(hidID.Value))
        {
            _TblSanPhamPhuKienManager.Delete(Convert.ToInt32(hidID.Value));
        }
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void btnApply_Click(object sender, EventArgs e)
    {
        BindToData();
        Response.Redirect(ResolveUrl("~/AdminPortal/Pages/PhuKienEdit.aspx?id=") + hidID.Value);
    }

    private void BindToData()
    {
        if (txtName.Text == "") { lblthongbao.Text = "<img src='../../Images/icon/Icon_warning.gif' alt='Cảnh báo' /> <span style='font-size:11px;color:red;'>  Nhập tên phụ kiện !</span>"; txtName.Focus(); return; }
        TblSanPhamPhuKienEntity _TblSanPhamPhuKienEntity = new TblSanPhamPhuKienEntity();
        if (!string.IsNullOrEmpty(hidID.Value)) _TblSanPhamPhuKienEntity.Id = Convert.ToInt32(hidID.Value);//update
        _TblSanPhamPhuKienEntity.LangId = LangID;
        _TblSanPhamPhuKienEntity.TenPhuKien =Server.HtmlEncode( txtName.Text);
        if (!string.IsNullOrEmpty(txtGia.Text))
            _TblSanPhamPhuKienEntity.GiaBan = Convert.ToDouble(txtGia.Text);
        if (!string.IsNullOrEmpty(listSanPham.SelectedValue))
            _TblSanPhamPhuKienEntity.SanPhamId = Convert.ToInt32(listSanPham.SelectedValue);
        _TblSanPhamPhuKienEntity.TrangThai = (chkStatus.Checked == true) ? true : false;
        _TblSanPhamPhuKienEntity.SuDungGia = (chkGiaBan.Checked == true) ? true : false;
        _TblSanPhamPhuKienEntity.DanhMucSanPhamId = Convert.ToInt32(ddlDanhMucSanPham.SelectedValue);
        if (!string.IsNullOrEmpty(txtSoLuong.Text))
            _TblSanPhamPhuKienEntity.SoLuong = Convert.ToInt32(txtSoLuong.Text);
        else
            _TblSanPhamPhuKienEntity.SoLuong = 1;
        if (!string.IsNullOrEmpty(hidID.Value))
            _TblSanPhamPhuKienManager.Update(_TblSanPhamPhuKienEntity);//update
        else
        {
            _TblSanPhamPhuKienManager.Insert(_TblSanPhamPhuKienEntity);//Insert
            hidID.Value = _TblSanPhamPhuKienEntity.Id.ToString();
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect(SiteMap.CurrentNode.ParentNode.Url);
    }
    protected void ddlDanhMucSanPham_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            DataTable _dtSanPham = new TblSanPhamManager().be_selectToDropdownlist(Convert.ToInt32(ddlDanhMucSanPham.SelectedValue));
            if (_dtSanPham != null)
            {
                listSanPham.DataTextField = TblSanPhamFields.TenSanPham.Name;
                listSanPham.DataValueField = TblSanPhamFields.Id.Name;
                listSanPham.DataSource = _dtSanPham;
                listSanPham.DataBind();
            }
        }
        catch { }
    }
}